package com.njworkorder.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njworkorder.Entity.SysVisits;
import com.njworkorder.Mapper.SysVisitsMapper;
import com.njworkorder.Service.SysVisitsService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CompletableFuture;

@Service
public class SysVisitsServiceImpl extends ServiceImpl<SysVisitsMapper, SysVisits>
		implements SysVisitsService {

	@Async
	@Override
	public CompletableFuture<Boolean> incrementField() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String daeF = df.format(new Date());

		LambdaQueryWrapper<SysVisits> wrapper = new LambdaQueryWrapper<SysVisits>()
				.eq(SysVisits::getPublishTime, daeF);
		Long l = baseMapper.selectCount(wrapper);
		if(l == 0){
			SysVisits sysVisits = new SysVisits();
			sysVisits.setVisits(1);
			sysVisits.setPublishTime(new Date());
			return CompletableFuture.completedFuture(baseMapper.insert(sysVisits) > 0);
		}
		return CompletableFuture.completedFuture(baseMapper.incrementField(daeF) > 0);
	}
}
